<template>
  <el-dialog
    title="添加记录"
    :visible.sync="dialogVisible"
    width="600px"
    customClass="mes_dialog"
    v-dialogDrag
    :close-on-click-modal="false">
    <div class="edit_raw_item">
      <div class="raw_item">
        <div class="raw_label"><span class="raw_label_required">*</span>采购批次号</div>
        <div class="raw_input">
          <el-input v-model="purchaseNumber" maxLength="32" size="small" placeholder="请输入采购批次号"></el-input>
        </div>
      </div>
      <div class="raw_item"
           :key="item.key"
           v-for="item in editRawItem">
        <div class="raw_label" v-html="item.key"></div>
        <div class="raw_input">
          <el-input v-model="item.value" maxLength="32" size="small" placeholder="请输入"></el-input>
        </div>
      </div>
    </div>
    <div slot="footer" class="mes_btn">
      <el-button type="primary" @click="submit">提交</el-button>
      <el-button @click="_close">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'EditRawItem',
  data() {
    return {
      dialogVisible: false,
      templateId: '', // 模板id
      id: '', // 记录id
      purchaseNumber: '', // 采购批次号
      editRawItem: []
    }
  },
  methods: {
    // 关闭弹框
    _close() {
      this.dialogVisible = false
    },
    // 显示弹框
    openDialog(templateId, row) {
      this.templateId = templateId
      this.id = row.id
      this.editRawItem = JSON.parse(JSON.stringify(row.propertyValue))
      this.purchaseNumber = row.purchaseNumber
      this.dialogVisible = true
    },
    // 提交
    submit() {
      if (!this.purchaseNumber) {
        this.$message({
          type: 'error',
          message: '请填写采购批次号',
          duration: 1500,
          center: true
        })
      } else {
        this.$store.dispatch('saveData', {
          method: 'post',
          url: 'a/rawmaterial/rawMaterial/updateRawMaterial',
          data: {
            templateId: this.templateId,
            id: this.id,
            value: this.editRawItem.reduce((pre, cur) => [...pre, cur.value], [this.purchaseNumber]).join('@')
          },
          noCommit: true
        }).then(() => {
          this.dialogVisible = false
          this.$emit('saveSuccess')
          this.$message({
            type: 'success',
            message: '保存成功',
            duration: 1500,
            center: true
          })
        })
      }
    }
  }
}
</script>

<style scoped lang="less">
  .edit_raw_item{
    border: 1px solid #dbe1e8;
    .raw_item .raw_label{
      flex: 0 0 200px;
      text-align: right;
    }
  }
</style>
